//
//  BubbleSprite.m
//  SpaceStorm
//
//  Created by 祥 雍 on 13-7-12.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "BubbleSprite.h"

@implementation BubbleSprite

/**
	根据文件路径构造九宫格
	@param file 文件url
	@param size 九宫格的尺寸
	@param leftcap 左右两边保持不拉伸的宽度
	@param topcap 上下两边保持不拉伸的高度
	@returns 返回九宫格
 */
+(id)spriteWithFile:(NSString*)file
               size:(CGSize)size
            leftCap:(NSInteger)leftcap
             topCap:(NSInteger)topcap
{  
    return [[[self alloc] initWithFile:file size:size leftCap:leftcap topCap:topcap] autorelease];  
}  

/**
	根据文件路径初始化九宫格
	@param file 文件url
	@param size 九宫格的尺寸
	@param leftcap 左右两边保持不拉伸的宽度
	@param topcap 上下两边保持不拉伸的高度
	@returns 返回九宫格
 */
-(id)initWithFile:(NSString*)file
             size:(CGSize)size
          leftCap:(NSInteger)leftcap
           topCap:(NSInteger)topcap
{  
    UIImage* image = [UIImage imageNamed:file]; 
    
    return [self initWithImage:image size:size leftCap:leftcap topCap:topcap];
}  

/**
	根据frameName构造化九宫格
	@param frameName 
	@param size 九宫格的尺寸
	@param leftcap 左右两边保持不拉伸的宽度
	@param topcap 上下两边保持不拉伸的高度
	@returns 返回九宫格
 */
+(id)spriteWithFrameName:(NSString*)frameName
                    size:(CGSize)size
                 leftCap:(NSInteger)leftcap
                  topCap:(NSInteger)topcap
{
    return [[[self alloc] initWithFrameName:frameName size:size leftCap:leftcap topCap:topcap] autorelease];  
}

/**
	根据frameName初始化九宫格
	@param frameName 
	@param size 九宫格的尺寸
	@param leftcap 左右两边保持不拉伸的宽度
	@param topcap 上下两边保持不拉伸的高度
	@returns 返回九宫格
 */
-(id)initWithFrameName:(NSString*)frameName
                  size:(CGSize)size
               leftCap:(NSInteger)leftcap
                topCap:(NSInteger)topcap

{
    CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:frameName]; 
    UIImage* image = [self convertSpriteToImage:sprite];
    return [self initWithImage:image size:size leftCap:leftcap topCap:topcap];
}

/**
	根据UIImage初始化九宫格
	@param image UIImage的引用
	@param size 九宫格的尺寸
	@param leftcap 左右两边保持不拉伸的宽度
	@param topcap 上下两边保持不拉伸的高度
	@returns 返回九宫格
 */
-(id)initWithImage:(UIImage*)image
              size:(CGSize)size
           leftCap:(NSInteger)leftcap
            topCap:(NSInteger)topcap
{
    CGImageRef base = image.CGImage;   
    CGContextRef context = CGBitmapContextCreate(nil,  
                                                 size.width,   
                                                 size.height,   
                                                 CGImageGetBitsPerComponent(base),   
                                                 4 * size.width,  
                                                 CGImageGetColorSpace(base),   
                                                kCGImageAlphaPremultipliedLast | kCGBitmapByteOrderDefault);  
    //   retina屏幕的判断，以后做
    //        int  RETINA = 1;
    //        if (IS_RETINA == NO) 
    //        {
    //            RETINA = 1;
    //        }
    //        else
    //        {
    //            RETINA= 2;
    //        }  
    
    //        bubbleSize.width = bubbleSize.width * RETINA;
    //        bubbleSize.height = bubbleSize.height * RETINA;
    //        gap_X=gap_X * RETINA;
    //        gap_Y=gap_Y * RETINA;
    
    //        NSString  * nsPicName;
    //        if (RETINA==1)
    //        {
    //            nsPicName = @"grid9.png";
    //        }
    //        else
    //        {
    //            nsPicName = @"grid9.png";
    //        }
    
    float BASE_COL_X[] = {0,leftcap,leftcap + 1};  
    float BASE_ROW_Y[] = {0,topcap,topcap +1};  
    float BASE_COL_WIDTH[] = {leftcap,1,image.size.width - leftcap -1};  
    float BASE_ROW_HEIGHT[] = {topcap,1,image.size.height - topcap -1};  
    
    
    float TARGET_COL_WIDTH[] = {BASE_COL_WIDTH[0], size.width - BASE_COL_WIDTH[0] - BASE_COL_WIDTH[2], BASE_COL_WIDTH[2]};  
    
    float TARGET_ROW_HEIGHT[] = {BASE_ROW_HEIGHT[0], size.height - BASE_ROW_HEIGHT[0] - BASE_ROW_HEIGHT[2], BASE_ROW_HEIGHT[2]};  
    
    float TARGET_COL_X[] = {0,TARGET_COL_WIDTH[0],TARGET_COL_WIDTH[0]+TARGET_COL_WIDTH[1]};  
    
    float TARGET_ROW_Y[] = {size.height - TARGET_ROW_HEIGHT[0],  
        size.height - TARGET_ROW_HEIGHT[0] - TARGET_ROW_HEIGHT[1],  
        size.height - TARGET_ROW_HEIGHT[0] - TARGET_ROW_HEIGHT[1] - TARGET_ROW_HEIGHT[2]};  
    
    for (int row=0; row<3; row++)   
    {  
        for (int col=0; col<3; col++)  
        {  
            CGRect source = CGRectMake(BASE_COL_X[col], BASE_ROW_Y[row], BASE_COL_WIDTH[col], BASE_ROW_HEIGHT[row]);  
            
            if (source.origin.x<0)   
            {  
                source.origin.x=0;  
            }  
            if (source.origin.y<0)   
            {  
                source.origin.y=0;  
            }  
            if (source.size.width<0)   
            {  
                source.size.width=0;  
            }  
            if (source.size.height<0)   
            {  
                source.size.height=0;  
            }  
            
            CGRect target = CGRectMake(TARGET_COL_X[col], TARGET_ROW_Y[row], TARGET_COL_WIDTH[col], TARGET_ROW_HEIGHT[row]);  
            
            
            if (target.origin.x<0)   
            {  
                target.origin.x=0;  
            }  
            if (target.origin.y<0)   
            {  
                target.origin.y=0;  
            }  
            if (target.size.width<0)   
            {  
                target.size.width=0;  
            }  
            if (target.size.height<0)   
            {  
                target.size.height=0;  
            }  
            
            
            CGImageRef ref = CGImageCreateWithImageInRect(base, source);  
            
            if (ref)   
            {  
                CGContextDrawImage(context, target, ref);  
                CFRelease(ref);   
            }  
            
        }  
    }       
    CGImageRef final = CGBitmapContextCreateImage(context);   
    CGContextRelease(context);  
    return [super initWithCGImage:final key:nil];  
}

/**
	将Sprite转化为UIImage
	@param sprite 
	@returns 
 */
-(UIImage *) convertSpriteToImage:(CCSprite *)sprite
{
    CGPoint p = sprite.anchorPoint;   
    
    [sprite setAnchorPoint:ccp(0,0)];  
    
    CCRenderTexture *renderer = [CCRenderTexture renderTextureWithWidth:sprite.contentSize.width height:sprite.contentSize.height];
    
    [renderer begin];
    
    [sprite visit];  
    
    [renderer end];   
    
    [sprite setAnchorPoint:p];  
    
    return [UIImage imageWithData:[renderer getUIImageAsDataFromBuffer:kCCImageFormatPNG]];
}

@end
